package org.example.window;

import org.apache.flink.api.common.eventtime.TimestampAssignerSupplier;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.JoinFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.util.Arrays;

/**
 * @Author: tang
 * @Description:
 * @Date 2025/2/23 09:36
 */
public class WindowIntervalJoin {

    public static void main(String[] args) throws Exception{

        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();

        environment.setParallelism(1);

        WatermarkStrategy<Tuple2<String, Integer>> watermarkStrategy1 = WatermarkStrategy.<Tuple2<String, Integer>>forMonotonousTimestamps()
                .withTimestampAssigner((TimestampAssignerSupplier<Tuple2<String, Integer>>) context -> (element, recordTimestamp) -> element.f1 * 1000L);

        SingleOutputStreamOperator<Tuple2<String, Integer>> ds1 = environment.fromCollection(
                Arrays.asList(
                        Tuple2.<String, Integer>of("a", 1),
                        Tuple2.<String, Integer>of("a", 2),
                        Tuple2.<String, Integer>of("b", 10),
                        Tuple2.<String, Integer>of("b", 15)
                )
        ).assignTimestampsAndWatermarks(watermarkStrategy1);

        WatermarkStrategy<Tuple3<String, Integer,Integer>> watermarkStrategy2 = WatermarkStrategy.<Tuple3<String, Integer,Integer>>forMonotonousTimestamps()
                .withTimestampAssigner((TimestampAssignerSupplier<Tuple3<String, Integer,Integer>>) context -> (element, recordTimestamp) -> element.f1 * 1000L);

        SingleOutputStreamOperator<Tuple3<String, Integer, Integer>> ds2 = environment.fromCollection(
                Arrays.asList(
                        Tuple3.<String, Integer, Integer>of("a", 1, 2),
                        Tuple3.<String, Integer, Integer>of("a", 4, 3),
                        Tuple3.<String, Integer, Integer>of("b", 6, 4),
                        Tuple3.<String, Integer, Integer>of("b", 15, 5)
                )
        ).assignTimestampsAndWatermarks(watermarkStrategy2);

        KeyedStream<Tuple2<String, Integer>, String> keyedStream1 = ds1.keyBy(value -> value.f0);
        KeyedStream<Tuple3<String, Integer,Integer>, String> keyedStream2 = ds2.keyBy(value -> value.f0);

        /**
         * intervalJoin() 方法，用于实现两个流之间的连接，并指定连接的窗口大小。
         * keyedStream1 中的元素，根据事件时间上下浮动的时间范围，去匹配到 keyedStream2 中的元素。
         */
        SingleOutputStreamOperator<String> streamOperator = keyedStream1.intervalJoin(keyedStream2)
                .between(Time.seconds(-2), Time.seconds(2))
                .process(new ProcessJoinFunction<Tuple2<String, Integer>, Tuple3<String, Integer, Integer>, String>() {
                    @Override
                    public void processElement(Tuple2<String, Integer> left, Tuple3<String, Integer, Integer> right,
                                               ProcessJoinFunction<Tuple2<String, Integer>, Tuple3<String, Integer, Integer>, String>.Context ctx,
                                               Collector<String> out) throws Exception {
                        out.collect(left + "--->" + right);
                    }
                });

        streamOperator.print();

        environment.execute();

    }

}
